#!/usr/bin/env python3
# -*- coding:UTF-8 -*-


import json
import re
import requests
from xpinyin import Pinyin
from util.BaseUtil import *


class xfpClient:

    def __init__(self):
        # 获取密钥
        r = requests.post('http://192.168.10.94:8381/api/client/getSecretKey')
        data = json.loads(r.text)
        self.secretKey = data['data']
        self.headers = {
            'secretKey': self.secretKey,
            'Content-Type': 'application/json'
        }

    # 获取咨询师列表
    def getConsultantList(self, data):
        try:
            r = requests.post('http://192.168.10.94:8381/api/client/consultant/list', headers=self.headers, data=json.dumps(data))
            data = json.loads(r.text)
            consultantList = data['data']['records']
            consultantBaseList = []
            print(type(consultantList), consultantList)
            for obj in consultantList:
                consultantObj = {}
                for key in obj:
                    if (key == 'consultantName'):
                        consultantObj['name'] = NoneToStr(obj[key])
                    if (key == 'consultantPhone'):
                        consultantObj['phone'] = NoneToStr(obj[key])
                consultantBaseList.append(consultantObj)
            print('count:', len(consultantBaseList), consultantBaseList)
        except Exception as e:
            print('系统内部异常', e)

        # 获取咨询师列表

    def saveClue(self):
        try:
            pinyin = Pinyin()
            form = {}
            label = random_label()
            labelNo = list(label.keys())[0]
            labelName = list(label.values())[0]
            name = random_name()
            phone = random_phone()
            remark = '来源:' + labelName + '<br/>站点:www.' + pinyin.get_pinyin(name, '') + '.com'
            form['labelNo'] = labelNo
            form['clueNickName'] = name
            form['cluePhone'] = phone
            form['saasCode'] = 'admin'
            form['remark'] = remark

            print(form)
            r = requests.post('http://192.168.10.94:8381/api/client/clue/save', headers=self.headers, data=json.dumps(form))
            data = json.loads(r.text)
            print(data)

        except Exception as e:
            print('系统内部异常', e)

    def get2(self):
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
            }
            # 这里要加浏览器标识信息，否则知乎会禁止抓取
            r = requests.get("https://www.zhihu.com/explore", headers=headers)

            print(type(r.status_code), '状态码:', r.status_code)  # 状态码
            print(type(r.headers), '请求头:', r.headers)  # 请求头
            print(type(r.cookies), 'cookies:', r.cookies)  # cookies
            print(type(r.url), 'url:', r.url)
            print(type(r.history), '请求历史:', r.history)  # 请求历史

            pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>', re.S)
            titles = re.findall(pattern, r.text)
            print(titles)
        except Exception as e:
            print('系统内部异常', e)


# 主函数入口
if __name__ == '__main__':
    client = xfpClient()
    client.saveClue()
